<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="showDialog"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <NewPageFilter
      v-model:query="filterInfo.query"
      :query-more-json="false"
      :filter-list="filterInfo.searchList"
      :filter-btn="filterInfo.searchBtn"
    >
      <template v-slot:filter-arrivalTime>
        <el-date-picker
          v-model="filterInfo.arrivalTime"
          format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          :clearable="true"
          :picker-options="$fn.pickerOptionsFn()"
          align="right"
          start-placeholder="到达开始时间"
          end-placeholder="到达结束时间"
          :shortcuts="$fn.pickerOptionsFn()"
          @change="timeChange"
        ></el-date-picker>
      </template>
    </NewPageFilter>
    <DialogTable
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="tableInfo.api"
      :query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :tabIndex="true"
      :checkBox="false"
      :reserve-selection="true"
      @handleEvent="handleEvent"
    ></DialogTable>
  </page-dialog>
</template>
<script setup name="needDealt">
import DialogTable from '@/components/DialogTable/index'
import NewPageFilter from '@/components/NewPageFilter/index'
import { getScheduleTaskList } from '@/api/core'
const route = useRoute()
const emits = defineEmits()
const { proxy } = getCurrentInstance()

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
})

// 过滤相关配置
const filterInfo = ref({
  query: {},
  arrivalTime: [],
  searchList: [
    {
      componentType: 'input',
      label: '任务标题',
      value: 'title',
      event: search,
    },
    {
      componentType: 'input',
      label: '任务描述',
      value: 'subTitle',
      event: search,
    },
    {
      slot: true,
      label: '到达时间',
      value: 'arrivalTime',
    },
  ],
  searchBtn: [{ type: 'button', label: '搜索', event: search }],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  fieldList: [
    { label: '任务类型', value: 'scheduleTaskTypeName' },
    { label: '任务标题', value: 'title', minWidth: '200px' },
    { label: '任务描述', value: 'subTitle', minWidth: '200px' },
    { label: '任务到达时间', value: 'arrivalTime' },
    { label: '计划开始时间', value: 'planStartTime' },
    { label: '计划完成时间', value: 'planEndTime' },
    { label: '备注', value: 'remark' },
  ],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '100',
    btList: [
      {
        label: '处理',
        event: checkCalend,
        show: true,
        ifRender: (row) => {
          return row.taskType === 2
        },
      },
      {
        label: '审批',
        event: setFlowData,
        show: true,
        ifRender: (row) => {
          return row.taskType === 1
        },
      },
    ],
  },
})
// 弹窗相关
const dialogInfo = ref({
  title: '全部待办',
  width: '90%',
  btLoading: false,
  btList: [{ label: '关闭', event: handleClose, show: true }],
})
const showDialog = computed({
  get() {
    getList()
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件shows
  },
})
function checkCalend(data) {
  let url = data.detailUrl
  let name = ''
  let routes = proxy.$store.state.permission.routes
  routes.map((item) => {
    if (item.children && item.children.length) {
      item.children.map((i) => {
        if (i.url === url) {
          name = url
        }
      })
    }
  })
  data.urlType = 2
  // if(data.detailUrl.indexOf('/asset/assetManage/assetInstrumentsPlan') !== -1){
  //   data.urlType = 3
  // }
  if (name) {
    proxy.$router.push(url)
    proxy.$store.dispatch('settings/toPage', data)
    showDialog.value = false
  } else {
    proxy.$message({
      duration: 3500,
      type: 'warning',
      showClose: true,
      message: '你没有相应的菜单权限',
    })
  }
}
function setFlowData(row) {
  proxy.$fn.setFlowData(row, 'myPending', route)
}
function timeChange(val) {
  if (val && val.length) {
    filterInfo.value.query.arrivalStartTime = val[0]
    filterInfo.value.query.arrivalEndTime = val[1]
  } else {
    filterInfo.value.query.arrivalStartTime = ''
    filterInfo.value.query.arrivalEndTime = ''
  }
  search()
}
// 获取列表
function getList() {
  proxy.$nextTick(() => {
    tableInfo.value.api = getScheduleTaskList
    tableInfo.value.refresh = Math.random()
  })
}
function search() {
  tableInfo.value.api = getScheduleTaskList
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function handleClose() {
  showDialog.value = false
}
</script>

<style scoped lang="scss"></style>
